Hey there.
Welcome.
In this lecture, we'll be scripting our final trap, which is a shotgun hidden in a tree.
When our player steps on a pressure plate, it's going to trigger the shotgun and kill our player.
So if I go walk over here and trigger a pressure plate, there you go.
The shotgun shoots and kills me.
So let's go ahead and get started.
Same procedure as before.
I'll create a new module script in the handler section for my starter player scripts, and I'm going
to call this my shotgun trap handler.
Inside of here.
We'll paste in that same template as before, and I'll rename this to Shotgun Trap Handler.
We'll get rid of the start function.
We'll grab the collection service.
And then we'll go ahead and define the tag for this module.
And the tag for our shotgun trap is just shotgun trap.
And then we can go ahead and create the other module script to act as our class.
I'm going to call this the Shotgun trap class.
We'll paste in that class template.
We'll rename it to Shotgun Trap Class.
And then we can go ahead and require that class within our handler.
So shotgun trap class is equal to require script dot shotgun trap class.
And then we'll listen to the collection service get instance added signal.
Pass our tag, connect a function, get the instance, and then use our shotgun trap class dot new constructor.
On this instance, boom!
We already know what to do.
Then in here I'll rename this to our Shotgun Trap objects.
And then we can go ahead and grab some services.
One is going to be the sound service to grab that shotgun sound.
We'll need the player service.
And they're also going to get the tween service to tween that pressure plate up and down when a player
touches it.
Then we can go ahead and make a couple references.
One is going to be for the local player.
So players local player will make a reference to the shotgun shoot sound and that's in sound service
SFX shotgun shoot.
And then we'll also get the pressure plate sound.
So pressure plate sound that's in sound service SFX dot pressure plate.
Then inside of our constructor, we could go ahead and make a reference to our model SCL two model.
We'll get the pressure plate and inside of our model that part is just called pressure plate.
We'll also go ahead and get the, um.
Emitter.
There's a particle emitter for emitting a flash.
So that's going to be our muzzle flash.
So I'll call this muzzle flash emitter.
And that's located inside of our model.
We'll get the shotgun.
And inside of the shotgun there's an attachment named Attachment Muzzle.
And inside of that muzzle we have a muzzle flash emitter.
And then we also have a light instance in there to like flicker.
So it looks like the shotgun shot and lit up the area around it.
So we'll just call this light.
And that's equal to model dot shotgun.
And inside of the attachment muzzle we also have a point light instance.
And last but not least we can have a Debounce called on cooldown and set that equal to false.
Then inside of our model we have another part called Detect Zone.
We'll listen for when that gets touched, connect a function and get the other part that touched it.
And then again, we'll see if a player touched that part.
So players get player from character, pass other part parent.
And if we do not have a player then we'll just return.
Otherwise, we can go ahead and define a function for when the shotgun is triggered or the pressure
plate is triggered.
I'll create a function and I'll just call this triggered.
And we'll call this function from this lambda function.
Inside of our triggered function.
What we can go ahead and do is check if we're on cooldown.
If we are, then we'll return.
Otherwise, set on cooldown equal to true and then we can go ahead and clone our pressure plate sound.
So I'll just call it pressure sound.
And that's equal to pressure plate sound clone.
And we'll set play on remove equal to true.
The parent is going to be equal to self dot model dot pressure plate.
And then we'll just destroy the sound to play the pressure plate sound.
And then afterwards, we want to go ahead and tween the pressure plate to move downwards to look like
we stepped on it.
So tween service will create a new tween on self dot model dot pressure plate.
We'll do a tween info dot new.
We'll play for like 0.5 seconds.
It doesn't really matter how long you want to do it.
And we're going to update the position of the pressure plate equal to self dot model, dot pressure
plate dot position.
And then we're going to offset it with a new vector three.
And we're going to move it um minus one in the y direction.
And then we can just play this tween.
And then afterwards we can wait for the duration of this tween which is 0.5 seconds.
Or we can also just store the tween in a variable and listen to when the event completes instead.
Either way works.
So we'll do it this way.
So when tween dot completed fires, we're going to wait for that event.
And then once that event fires, we're going to clone the shoot sound.
So we'll call this shoot sound.
And that's equal to shotgun shoot sound.
Clone that shoot sound.
We're going to set play on remove also equal to true.
The parent is going to be equal to uh self dot model dot shotgun.
And then we can just set it uh to the attachment.
So attachment muzzle and then we'll destroy the sound instance.
And then once we do that, we can go ahead and emit that muzzle flash from the particle emitter.
So self dot muzzle flash emitter, we're going to emit one particle.
And then we're also going to enable the light in our shotgun.
And since we want this light to flicker on and then back off we can delay a function.
For a very short duration, and that duration is going to be how long our muzzle flash exists for,
so we can refer to our muzzle flash emitter.
And then we can refer to the lifetime property, which is a number sequence, and then get the max value
in there.
And then once that time is up, we can go ahead and set self dot light dot enabled back equal to false.
And then we also want to go ahead and kill the player.
So local player.
Dot character dot humanoid.
We want to change the state equal to the enum dot humanoid state type dot dead.
After all of that, then we can go ahead and, um, we can wait for, I guess, the shotgun shoot sound
to end.
So shotgun, uh, shoot sound.
We'll get the time length, because it's going to be the exact same as our shoot sound.
We're going to wait for that duration.
And I want that duration is up.
We'll create a tween back on our pressure plate to move it back to its original position.
So we'll refer to self dot model dot pressure plates between info dot new 0.5 seconds.
And then we're going to set the position of the pressure plate equal to self dot model dot pressure
plate dot position.
And then we can offset it again with a new vector three.
But this time we're going to move it back to its original position which is one stud upwards in the
y direction.
And then we play that tween and then we can set self on cooldown back to false.
Okay cool.
So now any time our player goes and touches a pressure plate associated with a shotgun trap, we should
move the pressure plate downwards.
It'll play the shoot sound.
It'll emit a flash of light and it'll emit a particle.
It'll kill our player.
And then it should reset itself.
So it's time to test to see if it works.
So we'll play test our game here, and I'll get back to you when I find a shotgun trap.
Alrighty, here is one of our shotgun traps right there.
And I believe the pressure plate is this stone right here.
So if I go and walk and touch this plate here, it should move down and the shotgun should kill me.
So if I step on it.
It played the sound but unfortunately looked like nothing happened.
So let's go ahead and see exactly what we did wrong.
I don't think we played the tween, which is our issue.
Yeah.
So we forgot to play the tween right here.
Make sure you actually play the tween before you wait to listen for the tween to complete.
Very important to do.
Okay.
Let's go ahead and try that again.
But this time to make our odds easier of finding one of these shotgun traps.
We're just going to go ahead and increase the chance of, for example, this model.
I'll just set it to like 100.
So that way we're guaranteed of finding a shotgun trap on the first try.
So there we go.
There's a shotgun trap.
And the pressure plate should be one of these stones over here.
So I'll just walk around over here.
There we go.
The pressure plate went down and then we got shotgunned in our face.
Very cool.
So if we go.
And try it again.
Hopefully it should be reset and it should kill us again.
There we go.
Just like that, we've got our nice little hidden shotgun trap.
All right.
We're done with all of the traps for our project.
We just have a couple more things to add and we'll be finished.
So I'll see you in the next lecture.